package hr.web.jsf.converters;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

@Name("listIConverter")
@BypassInterceptors
@org.jboss.seam.annotations.faces.Converter
public class ListIConverter implements Converter {

	@Override
	public Object getAsObject(final FacesContext arg0, final UIComponent arg1,
			final String arg2) {
		final List<Integer> parameters = new ArrayList<Integer>();
		final StringTokenizer st = new StringTokenizer(arg2, ",");

		while (st.hasMoreElements()) {
			parameters.add(Integer.parseInt(st.nextToken()));
		}
		return parameters;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getAsString(final FacesContext arg0, final UIComponent arg1,
			final Object arg2) {
		final List<Integer> parameters = (List) arg2;
		String p = "";
		for (final Integer integer : parameters) {
			final String item = integer.toString();
			p += item + ",";
		}
		return p;
	}

}
